/** @file oct_leaf.cpp
    @brief Octree leaf */

#include "oct/oct_leaf.h"

using namespace G3;

//==============================================================================
// Constructor(s)
//==============================================================================
G3::OCT_LEAF::OCT_LEAF () {
    leaf_vis_cache = G3::GFC_OUT;

    log = &G3::MainLog;

    num_batches = 0;
    batch_offset = 0;
}

//==============================================================================
// A copy-constructor
//==============================================================================
G3::OCT_LEAF::OCT_LEAF (const OCT_LEAF &leaf) {
    BBox = leaf.BBox;

    leaf_vis_cache = leaf.leaf_vis_cache;

    log = leaf.log;

    num_batches = leaf.num_batches;
    batch_offset = leaf.batch_offset;
}

//==============================================================================
// Destructor(s)
//==============================================================================
G3::OCT_LEAF::~OCT_LEAF () {
    clear ();
}

//==============================================================================
// Assignment operator
//==============================================================================
G3::OCT_LEAF &G3::OCT_LEAF::operator= (const G3::OCT_LEAF &leaf) {
    BBox = leaf.BBox;

    leaf_vis_cache = leaf.leaf_vis_cache;

    log = leaf.log;

    num_batches = leaf.num_batches;
    batch_offset = leaf.batch_offset;

    return *this;
}

//==============================================================================
// Clears the node
//==============================================================================
void G3::OCT_LEAF::clear () {
    // 'sup?
}

//==============================================================================
// Dumps the node
//==============================================================================
void G3::OCT_LEAF::dump () {
    if (log) {
        log->Report ("BBox: (%f, %f, %f) (%f, %f, %f)\n",
            BBox.Minimum.x, BBox.Minimum.y, BBox.Minimum.z, BBox.Maximum.x, BBox.Maximum.y, BBox.Maximum.z);

        log->Report ("Number of batches: %d\n", num_batches);
        log->Report ("Batch offset: %d\n", batch_offset);
        log->Report ("Visibility cache: %d\n", leaf_vis_cache);
    }
}

int G3::OCT_LEAF::upload (std::vector<G3::BATCH> &batches, const G3::VEC3F &pos) const {
    for (int i=0; i<num_batches; i++) {
        if (batch_offset + i < (int) batches.size ()) {
            batches [batch_offset + i].genKey (pos);

            G3::MainBatchSorter.add (&batches [batch_offset + i]);
        }
    }

    return 1;
}

int G3::OCT_LEAF::upload_removal (std::vector<G3::BATCH> &batches) const {
    for (int i=0; i<num_batches; i++) {
        if (batch_offset + i < (int) batches.size ())
            G3::MainBatchSorter.remove (&batches [batch_offset + i]);
    }

    return 1;
}
